home *** CD-ROM | disk | FTP | other *** search
/ The AGA Experience 3 / AGA Experience Volume 3 (1997)(NFA - SAdENESS)[!].iso / software / utilities / graphics / rtgmaster / demos / ball.c next >
Encoding:
C/C++ Source or Header  |  1996-04-28  |  3.6 KB  |  155 lines

  1.  
  2. // Little demo of rtgmaster.library
  3. // written by Wolfram Schenk
  4.  
  5. #include <rtgmaster/rtgmaster.h>
  6. #include <rtgmaster/rtgsublibs.h>
  7. #include <rtgmaster/rtgc2p.h>
  8. #include <exec/memory.h>
  9. #include <clib/rtgmaster_protos.h>
  10. #include <pragmas/rtgmaster_pragmas.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <proto/exec.h>
  14. #include <proto/dos.h>
  15. #include <proto/utility.h>
  16. #include <math.h>
  17.  
  18. struct RtgScreen *RtgScreen;
  19. struct ScreenReq *sr;
  20. struct RTGMasterBase *RTGMasterBase;
  21. ULONG signal;
  22.  
  23. ULONG cmap[800];
  24. UBYTE *Memory=NULL;
  25. struct TagItem rtag[] = {
  26.     smr_ChunkySupport,  (1<<9),  // only 8-Bit!
  27.     smr_PlanarSupport, -1,
  28.     smr_Buffers,        2,
  29.     smr_TitleText,      (ULONG)"Small Ball Demo",
  30.     TAG_DONE,           NULL
  31. };
  32.  
  33. struct TagItem tacks[] = {
  34.     rtg_Buffers,        2,
  35.     TAG_DONE,           0
  36. };
  37.  
  38. #define SQRT_TABSIZE 1024
  39. int sqrt_tab[SQRT_TABSIZE*2];
  40.  
  41. void InitSqrtTab(void)
  42. {
  43.   int i;
  44.  
  45.   for(i = 0; i < SQRT_TABSIZE+1; i++)
  46.     sqrt_tab[i] = 255-sqrt((DOUBLE)i/SQRT_TABSIZE)*255;
  47.   for(; i < SQRT_TABSIZE*2; i++)
  48.     sqrt_tab[i] = 0;
  49. }
  50.  
  51. void DrawBall(struct RtgScreen *screen, int buffer,
  52.               int cx, int cy, int radius)
  53. {
  54.   int scrwidth, scrheight;
  55.   int x, y;
  56.   UBYTE *line1, *line2, *bufadr;
  57.   int maxlight;
  58.   static struct TagItem screendata[] =
  59.   {
  60.     grd_Width,  0,
  61.     grd_Height, 0,
  62.     TAG_DONE,   0,
  63.   };
  64.  
  65.   GetRtgScreenData(screen, screendata);
  66.   scrwidth  = screendata[0].ti_Data;
  67.   scrheight = screendata[1].ti_Data;
  68.   bufadr = Memory;
  69.   /* Grundregel Nummer 1: Immer zeilenweise! */
  70.   for(y = 0; y <= radius; y++)
  71.   {
  72.     int sqr_y, sqr_radius;
  73.     line1= &bufadr[(cy+y)*scrwidth + cx];
  74.     line2= &bufadr[(cy-y)*scrwidth + cx];
  75.     sqr_y = y*y;
  76.     sqr_radius = radius*radius;
  77.     for(x = 0; x < radius; x++)
  78.       line1[x] = line1[-x] = line2[x] = line2[-x] = 
  79.         sqrt_tab[(x*x + sqr_y)*SQRT_TABSIZE/sqr_radius];
  80.   }
  81.   CallRtgC2P(RtgScreen,GetBufAdr(screen,buffer),Memory,signal,0,0,sr->Width,sr->Height,c2p_1x1);
  82.   Wait(1<<signal);
  83. }
  84.  
  85. void BallDemo(struct RtgScreen *screen)
  86. {
  87.   int size;
  88.   int scrwidth, scrheight;
  89.   int buffer;
  90.   static struct TagItem screendata[] =
  91.   {
  92.     grd_Width,  0,
  93.     grd_Height, 0,
  94.     TAG_DONE,   0,
  95.   };
  96.  
  97.   LockRtgScreen(RtgScreen);
  98.  
  99.   GetRtgScreenData(screen, screendata);
  100.   scrwidth  = screendata[0].ti_Data;
  101.   scrheight = screendata[1].ti_Data;
  102.  
  103.   buffer = 1;
  104.   for(size = 2; size*4 < scrwidth && size*4 < scrheight; size+= 4)
  105.   {
  106.     DrawBall(screen, buffer, scrwidth/2, scrheight/2, size);
  107.     SwitchScreens(screen, buffer);
  108.  
  109.     buffer = 1-buffer;
  110.   }
  111.  
  112.   UnlockRtgScreen(RtgScreen);
  113. }
  114.  
  115. void CloseStuff(char *errorString)
  116. {
  117.   if(errorString)
  118.     printf("Error: %s\n", errorString);
  119.   if(sr)             FreeRtgScreenModeReq(sr);
  120.   if(RtgScreen)      CloseRtgScreen(RtgScreen);
  121.   if(RTGMasterBase)  CloseLibrary((struct Library *) RTGMasterBase);
  122.   exit(0);
  123. }
  124.  
  125. void main(int argc, char **argv) {
  126.     unsigned int x, i;
  127.     if(!(RTGMasterBase = (struct RTGMasterBase *)OpenLibrary((STRPTR)"rtgmaster.library", 1)))
  128.       CloseStuff("Can't open rtgmaster.library!");
  129.    
  130.     signal=AllocSignal(-1);
  131.     if(!(sr = RtgScreenModeReq(rtag)))
  132.       CloseStuff("No screenmode!");
  133.     Memory=AllocMem(sr->Width*sr->Height,MEMF_CLEAR);
  134.     if(!(RtgScreen = OpenRtgScreen(sr, tacks)))
  135.       CloseStuff("Can't open screen!");
  136.  
  137.     cmap[0] = 256<<16+0;
  138.     x = 1;
  139.     for (i = 0; i < 256; i++) {
  140.       cmap[x++] = i*0x1000000;
  141.       cmap[x++] = 0;
  142.       cmap[x++] = 0;
  143.     }
  144.     cmap[x++]=0;
  145.  
  146.     LoadRGBRtg(RtgScreen, cmap);
  147.     InitSqrtTab();
  148.  
  149.     BallDemo(RtgScreen);
  150.     Delay(500);
  151.     FreeMem(Memory,sr->Width*sr->Height);
  152.     FreeSignal(signal);
  153.     CloseStuff(NULL);
  154. }
  155.